home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / chrome / pippki.jar / content / pippki / viewCertDetails.js < prev    next >
Encoding:
Text File  |  2007-10-01  |  12.0 KB  |  351 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is mozilla.org code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Bob Lord <lord@netscape.com>
  23.  *   Ian McGreer <mcgreer@netscape.com>
  24.  *   Javier Delgadillo <javi@netscape.com>
  25.  *   Kai Engert <kengert@redhat.com>
  26.  *   Kaspar Brand <mozcontrib@velox.ch>
  27.  *
  28.  * Alternatively, the contents of this file may be used under the terms of
  29.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  30.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31.  * in which case the provisions of the GPL or the LGPL are applicable instead
  32.  * of those above. If you wish to allow use of your version of this file only
  33.  * under the terms of either the GPL or the LGPL, and not to allow others to
  34.  * use your version of this file under the terms of the MPL, indicate your
  35.  * decision by deleting the provisions above and replace them with the notice
  36.  * and other provisions required by the GPL or the LGPL. If you do not delete
  37.  * the provisions above, a recipient may use your version of this file under
  38.  * the terms of any one of the MPL, the GPL or the LGPL.
  39.  *
  40.  * ***** END LICENSE BLOCK ***** */
  41.  
  42. const nsIX509Cert = Components.interfaces.nsIX509Cert;
  43. const nsIX509Cert3 = Components.interfaces.nsIX509Cert3;
  44. const nsX509CertDB = "@mozilla.org/security/x509certdb;1";
  45. const nsIX509CertDB = Components.interfaces.nsIX509CertDB;
  46. const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
  47. const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
  48. const nsIPKIParamBlock = Components.interfaces.nsIPKIParamBlock;
  49. const nsIASN1Object = Components.interfaces.nsIASN1Object;
  50. const nsIASN1Sequence = Components.interfaces.nsIASN1Sequence;
  51. const nsIASN1PrintableItem = Components.interfaces.nsIASN1PrintableItem;
  52. const nsIASN1Tree = Components.interfaces.nsIASN1Tree;
  53. const nsASN1Tree = "@mozilla.org/security/nsASN1Tree;1"
  54.  
  55. var bundle;
  56.  
  57. function AddCertChain(node, chain, idPrefix)
  58. {
  59.   var idfier = idPrefix+"chain_";
  60.   var child = document.getElementById(node);
  61.   var numCerts = chain.length;
  62.   var currCert;
  63.   var displayVal;
  64.   var addTwistie;
  65.   for (var i=numCerts-1; i>=0; i--) {
  66.     currCert = chain.queryElementAt(i, nsIX509Cert);
  67.     if (currCert.commonName) {
  68.       displayVal = currCert.commonName;
  69.     } else {
  70.       displayVal = currCert.windowTitle;
  71.     }
  72.     if (0 == i) {
  73.       addTwistie = false;
  74.     } else {
  75.       addTwistie = true;
  76.     }
  77.     child = addChildrenToTree(child, displayVal, currCert.dbKey,addTwistie);
  78.   }
  79. }
  80.  
  81. function AddUsage(usage,verifyInfoBox)
  82. {
  83.   var text  = document.createElement("textbox");
  84.   text.setAttribute("value", usage);
  85.   text.setAttribute("style", "margin: 2px 5px");
  86.   text.setAttribute("readonly", "true");
  87.   text.setAttribute("class", "scrollfield");
  88.   verifyInfoBox.appendChild(text);
  89. }
  90.  
  91. function setWindowName()
  92. {
  93.   //  Get the cert from the cert database
  94.   var certdb = Components.classes[nsX509CertDB].getService(nsIX509CertDB);
  95.   var myName = self.name;
  96.   bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  97.   var cert;
  98.  
  99.   var certDetails = bundle.GetStringFromName('certDetails');
  100.   if (myName != "") {
  101.     document.title = certDetails + '"' + myName + '"'; // XXX l10n?
  102.     //  Get the token
  103.     //  XXX ignore this for now.  NSS will find the cert on a token
  104.     //      by "tokenname:certname", which is what we have.
  105.     //var tokenName = "";
  106.     //var pk11db = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
  107.     //var token = pk11db.findTokenByName(tokenName);
  108.  
  109.     //var cert = certdb.findCertByNickname(token, myName);
  110.     cert = certdb.findCertByNickname(null, myName);
  111.   } else {
  112.     var pkiParams = window.arguments[0].QueryInterface(nsIPKIParamBlock);
  113.     var isupport = pkiParams.getISupportAtIndex(1);
  114.     cert = isupport.QueryInterface(nsIX509Cert);
  115.     document.title = certDetails + '"' + cert.windowTitle + '"'; // XXX l10n?
  116.   }
  117.  
  118.   //
  119.   //  Set the cert attributes for viewing
  120.   //
  121.  
  122.   //  The chain of trust
  123.   var chain = cert.getChain();
  124.   AddCertChain("treesetDump", chain, "dump_");
  125.   DisplayGeneralDataFromCert(cert);
  126.   BuildPrettyPrint(cert);
  127.   
  128.   if (cert instanceof nsIX509Cert3)
  129.   {
  130.     cert.requestUsagesArrayAsync(
  131.             getProxyOnUIThread(new listener(),
  132.                                Components.interfaces.nsICertVerificationListener));
  133.   }
  134. }
  135.  
  136.  
  137. function addChildrenToTree(parentTree,label,value,addTwistie)
  138. {
  139.   var treeChild1 = document.createElement("treechildren");
  140.   var treeElement = addTreeItemToTreeChild(treeChild1,label,value,addTwistie);
  141.   parentTree.appendChild(treeChild1);
  142.   return treeElement;
  143. }
  144.  
  145. function addTreeItemToTreeChild(treeChild,label,value,addTwistie)
  146. {
  147.   var treeElem1 = document.createElement("treeitem");
  148.   if (addTwistie) {
  149.     treeElem1.setAttribute("container","true");
  150.     treeElem1.setAttribute("open","true");
  151.   }
  152.   var treeRow = document.createElement("treerow");
  153.   var treeCell = document.createElement("treecell");
  154.   treeCell.setAttribute("label",label);
  155.   if (value)
  156.     treeCell.setAttribute("display",value);
  157.   treeRow.appendChild(treeCell);
  158.   treeElem1.appendChild(treeRow);
  159.   treeChild.appendChild(treeElem1);
  160.   return treeElem1;
  161. }
  162.  
  163. function displaySelected() {
  164.   var asn1Tree = document.getElementById('prettyDumpTree').
  165.                      treeBoxObject.view.QueryInterface(nsIASN1Tree);
  166.   var items = asn1Tree.selection;
  167.   var certDumpVal = document.getElementById('certDumpVal');
  168.   if (items.currentIndex != -1) {
  169.     var value = asn1Tree.getDisplayData(items.currentIndex);
  170.     certDumpVal.value = value;
  171.   } else {
  172.     certDumpVal.value ="";
  173.   }
  174. }
  175.  
  176. function BuildPrettyPrint(cert)
  177. {
  178.   var certDumpTree = Components.classes[nsASN1Tree].
  179.                           createInstance(nsIASN1Tree);
  180.   certDumpTree.loadASN1Structure(cert.ASN1Structure);
  181.   document.getElementById('prettyDumpTree').
  182.            treeBoxObject.view =  certDumpTree;
  183. }
  184.  
  185. function addAttributeFromCert(nodeName, value)
  186. {
  187.   var node = document.getElementById(nodeName);
  188.   if (!value) {
  189.     value = bundle.GetStringFromName('notPresent');  
  190.   }
  191.   node.setAttribute('value',value)
  192. }
  193.  
  194.  
  195.  
  196. function listener() {
  197. }
  198.  
  199. listener.prototype.QueryInterface =
  200.   function(iid) {
  201.     if (iid.equals(Components.interfaces.nsISupports) ||
  202.         iid.equals(Components.interfaces.nsICertVerificationListener))
  203.         return this;
  204.  
  205.     throw Components.results.NS_ERROR_NO_INTERFACE;
  206.   }
  207.  
  208. listener.prototype.notify =
  209.   function(cert, result) {
  210.     DisplayVerificationData(cert, result);
  211.   }
  212.  
  213. function DisplayVerificationData(cert, result)
  214. {
  215.   document.getElementById("verify_pending").setAttribute("hidden", "true");
  216.  
  217.   if (!result || !cert)
  218.     return; // no results could be produced
  219.  
  220.   if (!(cert instanceof Components.interfaces.nsIX509Cert))
  221.     return;
  222.  
  223.   //  Verification and usage
  224.   var verifystr = "";
  225.   var o1 = {};
  226.   var o2 = {};
  227.   var o3 = {};
  228.  
  229.   if (!(result instanceof Components.interfaces.nsICertVerificationResult))
  230.     return;
  231.  
  232.   result.getUsagesArrayResult(o1, o2, o3);
  233.  
  234.   var verifystate = o1.value;
  235.   var count = o2.value;
  236.   var usageList = o3.value;
  237.   if (verifystate == cert.VERIFIED_OK) {
  238.     verifystr = bundle.GetStringFromName('certVerified');
  239.   } else if (verifystate == cert.CERT_REVOKED) {
  240.     verifystr = bundle.GetStringFromName('certNotVerified_CertRevoked');
  241.   } else if (verifystate == cert.CERT_EXPIRED) {
  242.     verifystr = bundle.GetStringFromName('certNotVerified_CertExpired');
  243.   } else if (verifystate == cert.CERT_NOT_TRUSTED) {
  244.     verifystr = bundle.GetStringFromName('certNotVerified_CertNotTrusted');
  245.   } else if (verifystate == cert.ISSUER_NOT_TRUSTED) {
  246.     verifystr = bundle.GetStringFromName('certNotVerified_IssuerNotTrusted');
  247.   } else if (verifystate == cert.ISSUER_UNKNOWN) {
  248.     verifystr = bundle.GetStringFromName('certNotVerified_IssuerUnknown');
  249.   } else if (verifystate == cert.INVALID_CA) {
  250.     verifystr = bundle.GetStringFromName('certNotVerified_CAInvalid');
  251.   } else { /* if (verifystate == cert.NOT_VERIFIED_UNKNOWN || == USAGE_NOT_ALLOWED) */
  252.     verifystr = bundle.GetStringFromName('certNotVerified_Unknown');
  253.   }
  254.   var verified=document.getElementById('verified');
  255.   verified.setAttribute("value", verifystr);
  256.   if (count > 0) {
  257.     var verifyInfoBox = document.getElementById('verify_info_box');
  258.     for (var i=0; i<count; i++) {
  259.       AddUsage(usageList[i],verifyInfoBox);
  260.     }
  261.   }
  262. }
  263.  
  264. function DisplayGeneralDataFromCert(cert)
  265. {
  266.   //  Common Name
  267.   addAttributeFromCert('commonname', cert.commonName);
  268.   //  Organization
  269.   addAttributeFromCert('organization', cert.organization);
  270.   //  Organizational Unit
  271.   addAttributeFromCert('orgunit', cert.organizationalUnit);
  272.   //  Serial Number
  273.   addAttributeFromCert('serialnumber',cert.serialNumber);
  274.   //  SHA1 Fingerprint
  275.   addAttributeFromCert('sha1fingerprint',cert.sha1Fingerprint);
  276.   //  MD5 Fingerprint
  277.   addAttributeFromCert('md5fingerprint',cert.md5Fingerprint);
  278.   // Validity start
  279.   addAttributeFromCert('validitystart', cert.validity.notBeforeLocalDay);
  280.   // Validity end
  281.   addAttributeFromCert('validityend', cert.validity.notAfterLocalDay);
  282.   
  283.   //Now to populate the fields that correspond to the issuer.
  284.   var issuerCommonname, issuerOrg, issuerOrgUnit;
  285.   issuerCommonname = cert.issuerCommonName;
  286.   issuerOrg = cert.issuerOrganization;
  287.   issuerOrgUnit = cert.issuerOrganizationUnit;
  288.   addAttributeFromCert('issuercommonname', issuerCommonname);
  289.   addAttributeFromCert('issuerorganization', issuerOrg);
  290.   addAttributeFromCert('issuerorgunit', issuerOrgUnit);
  291. }
  292.  
  293. function updateCertDump()
  294. {
  295.   var asn1Tree = document.getElementById('prettyDumpTree').
  296.                      treeBoxObject.view.QueryInterface(nsIASN1Tree);
  297.  
  298.   var tree = document.getElementById('treesetDump');
  299.   if (tree.currentIndex < 0) {
  300.     alert("No items are selected."); //This should never happen.
  301.   } else {
  302.     var item = tree.contentView.getItemAtIndex(tree.currentIndex);
  303.     var dbKey = item.firstChild.firstChild.getAttribute('display');
  304.     //  Get the cert from the cert database
  305.     var certdb = Components.classes[nsX509CertDB].getService(nsIX509CertDB);
  306.     var cert = certdb.findCertByDBKey(dbKey,null);
  307.     asn1Tree.loadASN1Structure(cert.ASN1Structure);
  308.   }
  309.   displaySelected();
  310. }
  311.  
  312. function getProxyOnUIThread(aObject, aInterface) {
  313.     var mainThread = Components.
  314.             classes["@mozilla.org/thread-manager;1"].
  315.             getService().mainThread;
  316.  
  317.     var proxyMgr = Components.
  318.             classes["@mozilla.org/xpcomproxy;1"].
  319.             getService(Components.interfaces.nsIProxyObjectManager);
  320.  
  321.     return proxyMgr.getProxyForObject(mainThread,
  322.             aInterface, aObject, 5);
  323.     // 5 == NS_PROXY_ALWAYS | NS_PROXY_SYNC
  324. }
  325.  
  326. function getCurrentCert()
  327. {
  328.   var realIndex;
  329.   var tree = document.getElementById('treesetDump');
  330.   if (tree.view.selection.isSelected(tree.currentIndex)
  331.       && document.getElementById('prettyprint_tab').selected) {
  332.     /* if the user manually selected a cert on the Details tab,
  333.        then take that one  */
  334.     realIndex = tree.currentIndex;    
  335.   } else {
  336.     /* otherwise, take the one at the bottom of the chain
  337.        (i.e. the one of the end-entity, unless we're displaying
  338.        CA certs) */
  339.     realIndex = tree.view.rowCount - 1;
  340.   }
  341.   if (realIndex >= 0) {
  342.     var item = tree.contentView.getItemAtIndex(realIndex);
  343.     var dbKey = item.firstChild.firstChild.getAttribute('display');
  344.     var certdb = Components.classes[nsX509CertDB].getService(nsIX509CertDB);
  345.     var cert = certdb.findCertByDBKey(dbKey,null);
  346.     return cert;
  347.   }
  348.   /* shouldn't really happen */
  349.   return null;
  350. }
  351.